<?php
/**
 *
 */

/**
 * Класс UpMysql -- простой класс для доступа к БД .
 *
 * По параметрам конфига совместим с DBAL Platonica
 *
 * @package Up
 * @author Lucky <lucky@jetstyle.ru>
 * @version $Id$
 */
class UpMysql
{
	var $db_name=NULL;
	var $db_host=NULL;
	var $db_user=NULL;
	var $db_password=NULL;
	var $db_encoding=NULL;

	var $ready=NULL;
	var $error=NULL;
	public function __construct ($config=array(), $init=true)
	{
		$this->config = $config;
		if ($init) $this->init();
	} // end of __construct
	public function init ($force=false)
	{
		if ($this->ready && !$force) return $this->ready;
		foreach ($this->config as $k=>$v) $this->$k = $v;
		return $this->ready = $this->_init();
	} // end of method init
	public function _init ()
	{
		if (!isset($this->link)) {
			$this->link = mysql_connect($this->db_host, $this->db_user, $this->db_password);
		}
		if (!$this->link) {
			$this->error = mysql_error();
			return false;
		}
		if (!mysql_select_db($this->db_name, $this->link)) {
			$this->error = mysql_error($this->link);
			return false;
		}
		if ($this->db_encoding) {
			$result = $this->query("SET NAMES ".$this->db_encoding);
		}
		return true;
	} // end of method _init
	public function query ($sql)
	{
		assert('$this->link');
		$res = mysql_query($sql, $this->link);
		if (!$res) $this->error = mysql_error($this->link);
		return $res;
	} // end of method query
	public function insert ($sql, $row=NULL)
	{
		assert('$this->link');
		$res = NULL;

		if ($row) {
			foreach ($row as $k=>$v) {
				$fields[] = "`".$this->escape($k)."`";
				$values[] = $v === NULL ? 'NULL' : "'".$this->escape($v)."'";
			}
			$fields_sql = implode(', ', $fields);
			$values_sql = implode(', ', $values);
			$sql = "INSERT INTO `{$sql}` ({$fields_sql}) VALUES ({$values_sql})";
		}

		return $this->query($sql);
	} // end of method query
	public function insertId ()
	{
		$res = mysql_insert_id($this->link);
		if (!$res) $this->error = mysql_error($this->link);
		return $res;
	} // end of method insertId
	public function update ($sql, $row=NULL, $pks=NULL)
	{
		assert('$this->link');
		$res = NULL;

		if ($row) {
			$where = array();
			if (isset($pks)) {
				if (!is_array($pks)) $pks = array($pks);
				foreach ($pks as $pk) {
					$f = "`".$this->escape($pk)."`";
					if (isset($row[$pk])) {
						$w = "'".$this->escape($row[$pk])."'";
						$op = ' = ';
					} else {
						$w = 'NULL';
						$op = ' IS ';
					}
					$where[] = "($f $op $w)";
					unset($row[$pk]);
				}
			}
			$fields = array();
			foreach ($row as $k=>$v) {
				$f = "`".$this->escape($k)."`";
				$w = $v === NULL ? 'NULL' : "'".$this->escape($v)."'";
				$fields[] = "$f = $w";
			}
			$fields_sql = implode(', ', $fields);
			$where_sql = $where ? 'WHERE '.implode(" AND ", $where) : '';
			$sql = "UPDATE `{$sql}` SET {$fields_sql} $where_sql";
		}
		//debug(__FUNCTION__ . " query: '$sql'");

		return $this->query($sql);
	} // end of method query
	public function escape ($str)
	{
		return mysql_real_escape_string($str, $this->link);
	} // end of method escape

	public function getOne ($rs)
	{
		return $rs ? mysql_fetch_assoc($rs) : false;
	} // end of method getOne
}
